package com.brayan.webapp.controller;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.inject.Inject;

import com.brayan.webapp.model.Pub;
import com.brayan.webapp.model.PubCategory;
import com.brayan.webapp.repository.PubCategoryRepository;
import com.brayan.webapp.repository.PubRepository;

@ManagedBean
@RequestScoped
public class PubCategoryController implements Serializable {

	private static final long serialVersionUID = 6555461789692254085L;

	@Inject
	private PubCategoryRepository pubCategoryRepository;
	@Inject
	private PubRepository pubRepository;	
	@Inject
	private PubCategory pubCategory;

	private List<Pub> listOfPublications;

	// Meta data
	private Long id;

	// Content
	private String name;

	public void init(Long pubCategoryId) {
		id = pubCategoryId;
		init();
	}

	public void init() {
		pubCategory = pubCategoryRepository.getPubCategoryById(getId());
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
		init();
	}
	
	public PubCategory getCategoryById(Long id){
		return pubCategoryRepository.getPubCategoryById(id);
	}

	public String getName() {
		if (pubCategory != null)
			name = pubCategory.getName();
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Pub> getListOfPublications() {
		if(getId() != null)
			listOfPublications = pubRepository.getPubsByCategory(getId());
		return listOfPublications;
	}

	public void setListOfPublications(List<Pub> listOfPublications) {
		this.listOfPublications = listOfPublications;
	}

}
